﻿using System;
using System.Collections.Generic;
using System.Text;

using Lucene.Net.Index;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using System.Text.RegularExpressions;
using com.crawler.common;
using System.IO;

namespace com.crawler.components
{
    public class LuceneManager
    {
        SysConfigInfo m_sys_config = null;
        static object m_obj = new object();

        /// <summary>
        /// 初始化系统配置
        /// </summary>
        public LuceneManager()
        {
            try
            {
                m_sys_config = SerializationHelper.Load(typeof(SysConfigInfo), Consts.SysConfigFileName) as SysConfigInfo;
            }
            catch (Exception ex)
            {
                LogHelper.Error("初始化系统配置失败" + ex.ToString());
            }
        }

        /// <summary>
        /// 添加图书索引
        /// </summary>
        /// <param name="i_book"></param>
        /// <returns></returns>
        public int AddBookIndex(BookInfo i_book)
        {
            return 1;
            string t_book_index_path = m_sys_config.BookIndexPath;

            if (string.IsNullOrEmpty(t_book_index_path))
                return -1;
            lock (m_obj)
            {
                if (!Directory.Exists(t_book_index_path))
                {
                    Directory.CreateDirectory(t_book_index_path);
                }

                IndexWriter t_write = null;

                t_write = new IndexWriter(t_book_index_path, new Lucene.Net.Analysis.Standard.StandardAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED);

                t_write.SetMergeFactor(1000);
                t_write.SetMaxBufferedDocs(1000);
                t_write.SetUseCompoundFile(false);

                try
                {
                    Document doc = new Document();

                    doc.Add(new Field("BOOKID", i_book.BookId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("BOOKNAME", i_book.BookName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("AUTHORNAME", i_book.AuthorName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("BOOKCATEGORY", i_book.CategoryName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("BOOKSUBCATEGORYNAME", i_book.SubCategoryName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("DESCRIPTION", i_book.Description, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LASTUPDATECHAPTERID", i_book.LastUpdateChapterId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LASTUPDATECHAPTERNAME", i_book.LastUpdateChapterName, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    Field t_field_updatetime = new Field("LASTUPDATETIME", i_book.LastUpdateChapterTime.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    doc.Add(t_field_updatetime);
                    Field t_field_book_chapter_count = new Field("BOOKCHAPTERCOUNT", i_book.ChapterCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    doc.Add(t_field_book_chapter_count);

                    doc.Add(new Field("SITEID", i_book.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("SITENAME", i_book.SiteName.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    t_write.AddDocument(doc);
                    t_write.Optimize();
                    t_write.Close();
                    return 1;

                }
                catch (Exception ex)
                {
                    LogHelper.Error("创建小说索引失败" + ex.ToString());
                    return -1;
                }
            }
        }

        /// <summary>
        /// 更新图书索引
        /// </summary>
        /// <param name="i_book"></param>
        /// <returns></returns>
        public int UpdateBookIndex(BookInfo i_book)
        {

            return 1;
            string t_book_index_path = m_sys_config.BookIndexPath;

            if (string.IsNullOrEmpty(t_book_index_path))
                return -1;
            lock (m_obj)
            {
                if (!Directory.Exists(t_book_index_path))
                {
                    Directory.CreateDirectory(t_book_index_path);
                }

                IndexWriter t_write = null;


                t_write = new IndexWriter(t_book_index_path, new Lucene.Net.Analysis.Standard.StandardAnalyzer(), false);


                try
                {
                    Term t_term = new Term("BOOKID", i_book.BookId.ToString());
                    Document doc = new Document();
                    doc.Add(new Field("BOOKID", i_book.BookId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("BOOKNAME", i_book.BookName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("AUTHORNAME", i_book.AuthorName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("CATEGORYNAME", i_book.CategoryName, Field.Store.YES, Field.Index.ANALYZED));
                    doc.Add(new Field("SUBCATEGORYNAME", i_book.SubCategoryName, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("DESCRIPTION", i_book.Description, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LASTUPDATECHAPTERID", i_book.LastUpdateChapterId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("LASTUPDATECHAPTERNAME", i_book.LastUpdateChapterName, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    Field t_field_updatetime = new Field("LASTUPDATETIME", i_book.LastUpdateChapterTime.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    doc.Add(t_field_updatetime);
                    Field t_field_book_chapter_count = new Field("BOOKCHAPTERCOUNT", i_book.ChapterCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED);
                    doc.Add(t_field_book_chapter_count);
                    doc.Add(new Field("SITEID", i_book.SiteId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    doc.Add(new Field("SITENAME", i_book.SiteName.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));



                    t_write.UpdateDocument(t_term, doc);
                   // t_write.Optimize();
                    t_write.Close();

                    return 1;

                }
                catch (Exception ex)
                {
                    LogHelper.Error("更新小说索引失败" + ex.ToString());
                    return -1;
                }
            }
        }

        //根据最后更新日期调节排序权重
        private float SetDateTimeBoost(DateTime i_last_update_time)
        {
            float t_boost = 0.0f;
            System.DateTime dt = i_last_update_time;
            System.DateTime now = System.DateTime.Now;

            System.TimeSpan ts = now - dt;
            int days = ts.Days;
            if (days <= 3)                         //小于3天
                t_boost = 134.297f;
            if (days > 3 && days <= 6)             //大于3天小于7天
                t_boost = 99.4665f;
            if (days > 6 && days <= 9)
                t_boost = 73.6788f;
            if (days > 9 && days <= 15)
                t_boost = 54.57693f;
            if (days > 15 && days <= 25)
                t_boost = 40.4274f;
            if (days > 25 && days <= 35)
                t_boost = 29.9462f;
            if (days > 35 && days <= 45)
                t_boost = 22.18236f;
            if (days > 45 && days <= 60)
                t_boost = 16.43138f;
            if (days > 60 && days <= 80)
                t_boost = 12.17139f;
            if (days > 80 && days <= 100)
                t_boost = 9.01584f;
            if (days > 100 && days <= 150)
                t_boost = 6.678405f;
            if (days > 150 && days <= 200)             //大于3天小于7天
                t_boost = 4.946967f;
            if (days > 200 && days <= 250)
                t_boost = 3.6644f;
            if (days > 250 && days <= 300)
                t_boost = 2.7143f;
            if (days > 300 && days <= 360)
                t_boost = 2.0106f;
            if (days > 360 && days <= 420)
                t_boost = 1.4896f;
            if (days > 420 && days <= 480)
                t_boost = 1.10324f;
            if (days > 480 && days <= 540)
                t_boost = 0.817215f;
            if (days > 540 && days <= 640)
                t_boost = 0.60534f;
            if (days > 640 && days <= 740)
                t_boost = 0.4484f;
            if (days > 740 && days <= 840)
                t_boost = 0.3321f;
            if (days > 840 && days <= 940)
                t_boost = 0.2464f;
            if (days > 940 && days <= 1040)
                t_boost = 0.1825f;
            if (days > 1040 && days <= 1500)
                t_boost = 0.135f;
            if (days > 1500)
                t_boost = 0.1f;
            return t_boost;
        }




    }
}
